package com.csl.jsonpreference.ui


import android.content.Context
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.view.inputmethod.InputMethodManager
import android.widget.ArrayAdapter
import androidx.lifecycle.lifecycleScope
import com.csl.jsonpreference.R
import com.csl.jsonpreference.databinding.DialogConfigTxtBinding
import com.csl.ui.base.CenterDialog
import com.csl.ui.base.launchResumeUI
import kotlinx.coroutines.delay

/**
 * 提示对话框
 */
open class TxtPreferenceDialog(
    var title: String,
    var descript: String = "",
    var content: String? = null,
    var contentValues: List<String>? = null,
    var listener: (String) -> Unit
) : CenterDialog() {

    var binding: DialogConfigTxtBinding? = null


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {

        getDialog()?.requestWindowFeature(Window.FEATURE_NO_TITLE); // 不显示标题栏
        binding = DialogConfigTxtBinding.inflate(inflater)
//        val dialogView: View =
//            inflater.inflate(R.layout.dialog_config_txt, container, false)
        initListener()


//        dialog?.requestWindowFeature(Window.FEATURE_NO_TITLE) // 不显示标题栏
        //        startUpAnimation(dialogView);
        return binding!!.root
    }

    override fun onStart() {
        super.onStart()
        initData()
        lifecycleScope.launchResumeUI {


            delay(500)
            binding?.jsonEtContent?.let {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    it.setFocusable(true)
                }
                it.isFocusableInTouchMode = true
                it.requestFocus()
                val imm =
                    it.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                imm.showSoftInput(it, 0)
                it.setSelection(it.text.length)
//                imm.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);
            }


        }

    }

    private fun initListener() {}

    private fun initData() {
        binding!!.apply {
            jsonTvTitle.text = "$title"
            jsonEtContent.setText("$content")
            jsonEtContent.visibility = View.VISIBLE
            jsonEtContent.hint = "$descript"
            jsonBtnCommit.setOnClickListener {
                listener(jsonEtContent.text.toString())
                dismiss()

            }
//            contentValues = ArrayList<String>().apply {
//                add("1")
//                add("2")
//                add("3")
//                add("4")
//                add("5")
//                add("6")
//            }
            contentValues?.let {
                this.jsonEtContent.setAdapter(
                    ArrayAdapter(
                        requireContext(),
                        R.layout.item_tip_txt,
                        R.id.json_tv_msg,
                        it
                    )
                )
                lifecycleScope.launchResumeUI {
                    binding?.jsonEtContent?.showDropDown()
                }
            }

            jsonBtnCommit.text = "修改"


            jsonBtnCancel.text = "取消"
            jsonBtnCancel.setOnClickListener {
                dismiss()
            }
            dialog?.setCancelable(true)
            dialog?.setCanceledOnTouchOutside(true)
        }

    }


    override fun onDestroy() {
        super.onDestroy()
        binding = null
    }
}